//
//  UserController.m
//  HappyBaby
//
//  Created by 华哥 on 15/11/23.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "UserController.h"
#import "UserTableViewCell.h"
#import "UserInfoCell.h"
#import "Define.h"
#import "UserHeadView.h"
#import "UserCenterController.h"
#import "SqliteManager.h"
#import "ChildrenStoryInterface.h"


@interface UserController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign) BOOL oneSection;
@property (nonatomic, assign) BOOL twoSection;

@property (nonatomic, assign) int childernStorySize;


@end

static NSString *tableViewHeadViewIndentifier = @"head";
static NSString *cellInfo = @"intable";
static NSString *cellIndentifer = @"cell";
@implementation UserController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 49) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UserTableViewCell class] forCellReuseIdentifier:cellIndentifer];
    [self.tableView registerClass:[UserInfoCell class] forCellReuseIdentifier:cellInfo];
    [self.tableView registerClass:[UserHeadView class] forHeaderFooterViewReuseIdentifier:tableViewHeadViewIndentifier];
    
    self.tableView.separatorStyle = NO;
    [self.view addSubview:_tableView];
}

#pragma mark ------------------ TableView设置 ----------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 0;
    }
    else if (section == 1)
    {
        if (self.oneSection == NO)
        {
            return 0;
        }
        else
        {
            return 2;
        }
    }
    else if (section == 2)
    {
        if (self.twoSection == NO)
        {
            return 0;
        }
        else
        {
            return 2;
        }
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 50;
    }
    else
    {
        return 40;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        UserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInfo forIndexPath:indexPath];
        cell.label.text = @"个人中心";
        cell.image.image = [UIImage imageNamed:@"iconfont-gerenzhongxin"];
        return cell;
    }
    else
    {
    UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifer forIndexPath:indexPath];
        if (indexPath.row == 0)
        {
            cell.image.image = [UIImage imageNamed:@"iconfont-shipin"];
            cell.label.text = @"缓存的儿歌";
            
        }
        else if (indexPath.row == 1)
        {
            cell.image.image = [UIImage imageNamed:@"iconfont-yinle"];
            cell.label.text = @"缓存的动画";
        }
    return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UserHeadView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:tableViewHeadViewIndentifier];
    if (section == 0)
    {
        headView.label.text = @"个人中心";
        headView.image.image = [UIImage imageNamed:@"iconfont-gerenzhongxin"];
        UITapGestureRecognizer *userCenterTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(userCenterAction)];
        [headView addGestureRecognizer:userCenterTap];
    }
    else if (section == 1)
    {
        headView.label.text = @"最近播放儿歌(已缓存)";
        headView.image.image = [UIImage imageNamed:@"iconfont-shipin"];
        UITapGestureRecognizer *oneTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(oneTap)];
        [headView addGestureRecognizer:oneTap];
    }
    else if (section == 2)
    {
        headView.label.text = @"最近播放的动画";
        headView.image.image = [UIImage imageNamed:@"iconfont-yinle"];
        UITapGestureRecognizer *twoTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(twoTap)];
        [headView addGestureRecognizer:twoTap];
    }
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (void)oneTap
{
    self.oneSection = !_oneSection;
    [self.tableView reloadData];
}
- (void)twoTap
{
    self.twoSection = !_twoSection;
    [self.tableView reloadData];
}

- (void)userCenterAction
{
    UserCenterController *userCenter = [[UserCenterController alloc]init];
    [self.navigationController pushViewController:userCenter animated:YES];
}

- (void)getmemoryFromSqilte
{
    SqliteManager *sql = [SqliteManager shareInstance];
    NSArray *array = [sql selectAllDataWithTableName:@"HappyBaby"];
    for (ChildrenStoryInterface *model in array)
    {
        self.childernStorySize += model.filesize;
    }    
}























@end
